const Controller = require('egg').Controller;
class BlogController extends Controller {
    async create(){
        const {ctx} = this;
        let {title,img,content,name} = ctx.request.body;
        if(!title || !img || !content || !name){
            ctx.body = {
                code:2,
                mes:'缺少参数'
            }
            return;
        }
        // 添加博客
        let res = await ctx.service.blog.addblog(ctx.request.body);
        if(res.affectedRows  == 1){
            ctx.body = {
                code:0,
                mes:'添加博客成功'
            }
        } else {
            ctx.body = {
                code:1,
                mes:'添加博客失败'
            }
        }
    }
    async destroy(){
        const {ctx} = this;
        let {id} = ctx.params;
        if(!id){
            ctx.body = {
                code:2,
                mes:'缺少参数'
            }
            return;
        }

        let res = await ctx.service.blog.delblog(id);
        if(res.affectedRows == 1){
            ctx.body = {
                code:0,
                mes:'删除成功'
            }
        } else {
            ctx.body = {
                code:1,
                mes:'删除失败'
            }
        }
    }
    async update(){
        // 修改 
        const {ctx} = this;
        let {title,img,content,name} = ctx.request.body;
        let {id} = ctx.params;
        console.log(id,'&&&&&&&&&&&&&&&')
        if(!title || !img || !content || !name || !id){
            ctx.body = {
                code:2,
                mes:'缺少参数'
            }
            return;
        }
        let res = await ctx.service.blog.updateblog({...ctx.request.body,id});
        if(res.affectedRows == 1){
            ctx.body = {
                code:0,
                mes:'修改成功'
            }
        } else {
            ctx.body = {
                code:1,
                mes:'修改失败'
            }
        }
    }
    async index(){
        const {ctx} = this;
        let res = await ctx.service.blog.listblog();
        if(res.length >= 0){
            ctx.body = {
                code:0,
                data:res
            }
        }
    }
}
module.exports = BlogController;